package com.nwpu.bigdata.util.impl;

import com.nwpu.bigdata.util.FuncUtil;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Service;

/**
 *@version:2018骞�7鏈�9鏃ヤ笂鍗�11:38:46
*/
/**
 * @author Administrator
 *
 */
@Service
@ImportResource(locations= {"classpath:/xmlConfig/spring/applicationContext-util.xml"})
public class FunctionImpl implements FuncUtil {
	@Autowired
	private InfluxDBUtilImpl influDBClient;
	
	@Value("${retentionPolicy.cluster}")
	private String k8sretentionPolicy;
	
	@Value("${inluxdb.k8s}")
	private String k8s;
	@Value("${retentionPolicy.cassandra}")
	private String retentionPolicy;
	
	@Value("${influxdb.cassandra}")
	private String db;
	@Override
	public Object avg(String type, String hostname, String metric, int param, String unit) {
		//求平均的方法
		InfluxdbService service = new InfluxdbService(k8s,k8sretentionPolicy,influDBClient.builder());
		//连接influxdb数据库
	    if(service.equals(null))			
	    {
	    	System.out.println(service.equals(null));
	    	return null;
	    }
	    //判断输入是否为空
		String str = null;
		//先将str设为空
		
		//若switch判断语句不可直接用字符串型（jdk1.7以前版本没有识别String的功能），则考虑用枚举型
		switch(type){
			case "cluster":
				if(unit == "num"){
					str =  "SELECT mean(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time > now() - "+(param+1)+"m";
				}else{
					str =  "SELECT mean(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time > now() - "+(param+1)+ unit;
				}
				break;
			case "ns":
				if(unit == "num"){
					str =  "SELECT mean(value) FROM \""+metric+"\" WHERE namespace_name = '"+hostname+"' and time > now() - "+(param+1)+"m";
				}else{
					str =  "SELECT mean(value) FROM \""+metric+"\" WHERE namespace_name = '"+hostname+"' and time > now() - "+(param+1)+ unit;
				} 
				break;
			case "pod":
			case "pod_container":
				if(unit == "num"){
					str =  "SELECT mean(value) FROM \""+metric+"\" WHERE pod_name = '"+hostname+"' and time > now() - "+(param+1)+"m";
				}else{
					str =  "SELECT mean(value) FROM \""+metric+"\" WHERE pod_name = '"+hostname+"' and time > now() - "+(param+1)+ unit;
				}
				break;
			case "node":
				if(unit == "num"){
					str =  "SELECT mean(value) FROM \""+metric+"\" WHERE nodename = '"+hostname+"' and time > now() - "+(param+1)+"m";
				}else{
					str =  "SELECT mean(value) FROM \""+metric+"\" WHERE nodename = '"+hostname+"' and time > now() - "+(param+1)+ unit;
				}
				break;
		}
			
		QueryResult queryResult = service.query(str);
		//将字符串传至influxdb，判断SQL语句
		Object result = null;
		if(queryResult.equals(null)){
			return 0.0;
		}else{
			try {
				 result = queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(1);
			} catch (Exception e) {
				return 0.0;
			}
		
		//将查询结果赋值给result
		influDBClient.builder().close();
		//关闭与influxdb的连接
		return result;//返回函数计算结果
		}
	}

	
	@Override
	public Object avg(int param, String unit, String hostname, String dbname, String metric) {
		//求平均的方法
		InfluxdbService service = new InfluxdbService(k8s,k8sretentionPolicy,influDBClient.builder());
		//连接influxdb数据库
		if(service.equals(null))			
		{
			System.out.println(service.equals(null));
			return null;
		}
		//判断输入是否为空
		String str = null;
		//先将str设为空

		if(unit == "num"){
			str =  "SELECT mean("+metric+") FROM \""+dbname+"\" WHERE hostname = '"+hostname+"' and time > now() - "+(param+1)+"m";
		}else{
			str =  "SELECT mean("+metric+") FROM \""+dbname+"\" WHERE hostname = '"+hostname+"' and time > now() - "+(param+1)+ unit;
		}
		QueryResult queryResult = service.query(str);
		//System.out.println(str);
		System.out.println(queryResult);
		Object result = null;
		//将字符串传至influxdb，判断SQL语句
		if(queryResult.equals(null)){
			influDBClient.builder().close();
			return 0.0;
		}else{
			try {
				result =  queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(1);
				
			} catch (Exception e) {
				influDBClient.builder().close();
				return 0.0;
			}
		
			
			influDBClient.builder().close();
			return result;
		//将查询结果赋值给result
		
		//关闭与influxdb的连接
		
		//return result;//返回函数计算结果
		}
		
	}

	
	@Override
	public Object count(String type, String hostname, String metric, int param, String unit) {
		//求平均的方法
		InfluxdbService service = new InfluxdbService(k8s,k8sretentionPolicy,influDBClient.builder());
		//连接influxdb数据库
		if(service.equals(null))			
		{
			System.out.println(service.equals(null));
			return null;
		}
		//判断输入是否为空
		String str = null;
		//先将str设为空

		//若switch判断语句不可直接用字符串型（jdk1.7以前版本没有识别String的功能），则考虑用枚举型
		switch(type){
		case "cluster":
			if(unit == "num"){
				str = "SELECT count(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time > now() - 1d and value =" + param;
			}else{
				str =  "SELECT count(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time > now() - "+(param+1)+ unit;
			}
			break;
		case "ns":
			if(unit == "num"){
				str = "SELECT count(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time > now() - 1d and value =" + param;
			}else{
				str =  "SELECT count(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time > now() - "+(param+1)+ unit;
			}
			break;
		case "pod":
		case "pod_container":
			if(unit == "num"){
				str = "SELECT count(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time > now() - 1d and value =" + param;
			}else{
				str =  "SELECT count(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time > now() - "+(param+1)+ unit;
			}
			break;
		case "node":
			if(unit == "num"){
				str = "SELECT count(value) FROM \""+metric+"\" WHERE time > now() - 1d and value =" + param;
			}else{
				str =  "SELECT count(value) FROM \""+metric+"\" WHERE time > now() - "+(param+1)+ unit;
			}
			break;
	}
			Object result = null;
		QueryResult queryResult = service.query(str);
		//将字符串传至influxdb，判断SQL语句
		if(queryResult.equals(null)){
			return 0.0;
		}else{
			try {
				result =  queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(1);
				
			} catch (Exception e) {
				influDBClient.builder().close();
				return 0.0;
			}
		
		//将查询结果赋值给result
		influDBClient.builder().close();
		//关闭与influxdb的连接
		return result;//返回函数计算结果
		}
	}

	
	@Override
	public Object count(int param, String unit, String hostname, String dbname, String metric) {
		//求平均的方法
		InfluxdbService service = new InfluxdbService(k8s,k8sretentionPolicy,influDBClient.builder());
		//连接influxdb数据库
		if(service.equals(null))			
		{
			System.out.println(service.equals(null));
			return null;
		}
		//判断输入是否为空
		String str = null;
		//先将str设为空

		if(unit == "num"){
			str = "SELECT count(value) FROM \""+metric+"\" WHERE time > now() - 1d and value =" + param;
		}else{
			str =  "SELECT count(value) FROM \""+metric+"\" WHERE time > now() - "+(param+1)+ unit;
		}
			//杩涜鏌ヨ
		Object result = null;
		QueryResult queryResult = service.query(str);
		//将字符串传至influxdb，判断SQL语句
		if(queryResult.equals(null)){
			return 0.0;
		}else{
			try {
				result =  queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(1);
				
			} catch (Exception e) {
				influDBClient.builder().close();
				return 0.0;
			}
		
		//将查询结果赋值给result
		influDBClient.builder().close();
		//关闭与influxdb的连接
		return result;//返回函数计算结果
		}
	}


	@Override
	public Object max(String type, String hostname, String metric, int param, String unit) {
		//求平均的方法
		InfluxdbService service = new InfluxdbService(k8s,k8sretentionPolicy,influDBClient.builder());
		//连接influxdb数据库
		if(service.equals(null))			
		{
			System.out.println(service.equals(null));
			return null;
		}
		//判断输入是否为空
		String str = null;
		//先将str设为空

		//若switch判断语句不可直接用字符串型（jdk1.7以前版本没有识别String的功能），则考虑用枚举型
		switch(type){
		case "cluster":
			if(unit == "num"){
				str =  "SELECT MAX(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time > now() - "+(param+1)+"m";
			}else{
				str =  "SELECT MAX(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time > now() - "+(param+1)+ unit;
			}
			break;
		case "ns":
			if(unit == "num"){
				str =  "SELECT MAX(value) FROM \""+metric+"\" WHERE namespace_name = '"+hostname+"' and time > now() - "+(param+1)+"m";
			}else{
				str =  "SELECT MAX(value) FROM \""+metric+"\" WHERE namespace_name = '"+hostname+"' and time > now() - "+(param+1)+ unit;
			} 
			break;
		case "pod":
		case "pod_container":
			if(unit == "num"){
				str =  "SELECT MAX(value) FROM \""+metric+"\" WHERE pod_name = '"+hostname+"' and time > now() - "+(param+1)+"m";
			}else{
				str =  "SELECT MAX(value) FROM \""+metric+"\" WHERE pod_name = '"+hostname+"' and time > now() - "+(param+1)+ unit;
			}
			break;
		case "node":
			if(unit == "num"){
				str =  "SELECT MAX(value) FROM \""+metric+"\" WHERE nodename = '"+hostname+"' and time > now() - "+(param+1)+"m";
			}else{
				str =  "SELECT MAX(value) FROM \""+metric+"\" WHERE nodename = '"+hostname+"' and time > now() - "+(param+1)+ unit;
			}
			break;
	}
		Object result = null;
		QueryResult queryResult = service.query(str);
		//将字符串传至influxdb，判断SQL语句
		if(queryResult.equals(null)){
			return 0.0;
		}else{
			try {
				result =  queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(1);
				
			} catch (Exception e) {
				influDBClient.builder().close();
				return 0.0;
			}
		
		//将查询结果赋值给result
		influDBClient.builder().close();
		//关闭与influxdb的连接
		return result;//返回函数计算结果
		}
	}

	
	@Override
	public Object max(int param, String unit, String hostname, String dbname, String metric) {
		//求平均的方法
		InfluxdbService service = new InfluxdbService(k8s,k8sretentionPolicy,influDBClient.builder());
		//连接influxdb数据库
		if(service.equals(null))			
		{
			System.out.println(service.equals(null));
			return null;
		}
		//判断输入是否为空
		String str = null;
		//先将str设为空

		if(unit == "num"){
			str =  "SELECT MAX("+metric+") FROM \""+dbname+"\" WHERE hostname = '"+hostname+"' and time > now() - "+(param+1)+"m";
		}else{
			str =  "SELECT MAX("+metric+") FROM \""+dbname+"\" WHERE hostname = '"+hostname+"' and time > now() - "+(param+1)+ unit;
		}
		Object result = null;
		QueryResult queryResult = service.query(str);
		//将字符串传至influxdb，判断SQL语句
		if(queryResult.equals(null)){
			return 0.0;
		}else{
			try {
				result =  queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(1);
				
			} catch (Exception e) {
				influDBClient.builder().close();
				return 0.0;
			}
		
		//将查询结果赋值给result
		influDBClient.builder().close();
		//关闭与influxdb的连接
		return result;//返回函数计算结果
		}
	}

	@Override
	public Object min(String type, String hostname, String metric, int param, String unit) {
		//求平均的方法
		InfluxdbService service = new InfluxdbService(k8s,k8sretentionPolicy,influDBClient.builder());
		//连接influxdb数据库
		if(service.equals(null))			
		{
			System.out.println(service.equals(null));
			return null;
		}
		//判断输入是否为空
		String str = null;
		//先将str设为空

		//若switch判断语句不可直接用字符串型（jdk1.7以前版本没有识别String的功能），则考虑用枚举型
		switch(type){
		case "cluster":
			if(unit == "num"){
				str =  "SELECT MIN(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time > now() - "+(param+1)+"m";
			}else{
				str =  "SELECT MIN(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time > now() - "+(param+1)+ unit;
			}
			break;
		case "ns":
			if(unit == "num"){
				str =  "SELECT MIN(value) FROM \""+metric+"\" WHERE namespace_name = '"+hostname+"' and time > now() - "+(param+1)+"m";
			}else{
				str =  "SELECT MIN(value) FROM \""+metric+"\" WHERE namespace_name = '"+hostname+"' and time > now() - "+(param+1)+ unit;
			} 
			break;
		case "pod":
		case "pod_container":
			if(unit == "num"){
				str =  "SELECT MIN(value) FROM \""+metric+"\" WHERE pod_name = '"+hostname+"' and time > now() - "+(param+1)+"m";
			}else{
				str =  "SELECT MIN(value) FROM \""+metric+"\" WHERE pod_name = '"+hostname+"' and time > now() - "+(param+1)+ unit;
			}
			break;
		case "node":
			if(unit == "num"){
				str =  "SELECT MIN(value) FROM \""+metric+"\" WHERE nodename = '"+hostname+"' and time > now() - "+(param+1)+"m";
			}else{
				str =  "SELECT MIN(value) FROM \""+metric+"\" WHERE nodename = '"+hostname+"' and time > now() - "+(param+1)+ unit;
			}
			break;
	}
		Object result = null;
		QueryResult queryResult = service.query(str);
		//将字符串传至influxdb，判断SQL语句
		if(queryResult.equals(null)){
			return 0.0;
		}else{
			try {
				result =  queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(1);
				
			} catch (Exception e) {
				influDBClient.builder().close();
				return 0.0;
			}
		
		//将查询结果赋值给result
		influDBClient.builder().close();
		//关闭与influxdb的连接
		return result;//返回函数计算结果
		}
	}

	
	@Override
	public Object min(int param, String unit, String hostname, String dbname, String metric) {
		//求平均的方法
		InfluxdbService service = new InfluxdbService(k8s,k8sretentionPolicy,influDBClient.builder());
		//连接influxdb数据库
		if(service.equals(null))			
		{
			System.out.println(service.equals(null));
			return null;
		}
		//判断输入是否为空
		String str = null;
		//先将str设为空

		if(unit == "num"){
			str =  "SELECT MIN("+metric+") FROM \""+dbname+"\" WHERE hostname = '"+hostname+"' and time > now() - "+(param+1)+"m";
		}else{
			str =  "SELECT MIN("+metric+") FROM \""+dbname+"\" WHERE hostname = '"+hostname+"' and time > now() - "+(param+1)+ unit;
		}
		Object result = null;
		QueryResult queryResult = service.query(str);
		//将字符串传至influxdb，判断SQL语句
		if(queryResult.equals(null)){
			return 0.0;
		}else{
			try {
				result =  queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(1);
				
			} catch (Exception e) {
				influDBClient.builder().close();
				return 0.0;
			}
		
		//将查询结果赋值给result
		influDBClient.builder().close();
		//关闭与influxdb的连接
		return result;//返回函数计算结果
		}
	}

	
	@Override
	public Object last(String type, String hostname, String metric, int param, String unit) {
		//求平均的方法
		InfluxdbService service = new InfluxdbService(k8s,k8sretentionPolicy,influDBClient.builder());
		//连接influxdb数据库
		if(service.equals(null))			
		{
			System.out.println(service.equals(null));
			return null;
		}
		//判断输入是否为空
		String str = null;
		//先将str设为空

		//若switch判断语句不可直接用字符串型（jdk1.7以前版本没有识别String的功能），则考虑用枚举型
		switch(type){
		case "cluster":
			if(unit == "num"){
				str =  "SELECT LAST(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time < now() - "+(param+1)+"m";
			}else{
				str =  "SELECT LAST(value) FROM \""+metric+"\" WHERE type = '"+type+"' and time < now() - "+(param+1)+ unit;
			}
			break;
		case "ns":
			if(unit == "num"){
				str =  "SELECT LAST(value) FROM \""+metric+"\" WHERE namespace_name = '"+hostname+"' and time < now() - "+(param+1)+"m";
			}else{
				str =  "SELECT LAST(value) FROM \""+metric+"\" WHERE namespace_name = '"+hostname+"' and time < now() - "+(param+1)+ unit;
			} 
			break;
		case "pod":
		case "pod_container":
			if(unit == "num"){
				str =  "SELECT LAST(value) FROM \""+metric+"\" WHERE pod_name = '"+hostname+"' and time < now() - "+(param+1)+"m";
			}else{
				str =  "SELECT LAST(value) FROM \""+metric+"\" WHERE pod_name = '"+hostname+"' and time < now() - "+(param+1)+ unit;
			}
			break;
		case "node":
			if(unit == "num"){
				str =  "SELECT LAST(value) FROM \""+metric+"\" WHERE nodename = '"+hostname+"' and time < now() - "+(param+1)+"m";
			}else{
				str =  "SELECT LAST(value) FROM \""+metric+"\" WHERE nodename = '"+hostname+"' and time < now() - "+(param+1)+ unit;
			}
			break;
	}
		Object result = null;
		QueryResult queryResult = service.query(str);
		//将字符串传至influxdb，判断SQL语句
		if(queryResult.equals(null)){
			return 0.0;
		}else{
			try {
				result =  queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(1);
				
			} catch (Exception e) {
				influDBClient.builder().close();
				return 0.0;
			}
		
		//将查询结果赋值给result
		influDBClient.builder().close();
		//关闭与influxdb的连接
		return result;//返回函数计算结果
		}
	}

	
	@Override
	public Object last(int param, String unit, String hostname, String dbname, String metric) {
		//求平均的方法
		InfluxdbService service = new InfluxdbService(k8s,k8sretentionPolicy,influDBClient.builder());
		//连接influxdb数据库
		if(service.equals(null))			
		{
			System.out.println(service.equals(null));
			return null;
		}
		//判断输入是否为空
		String str = null;
		//先将str设为空

		if(unit == "num"){
			str =  "SELECT LAST("+metric+") FROM \""+dbname+"\" WHERE hostname = '"+hostname+"' and time < now() - "+(param+1)+"m";
		}else{
			str =  "SELECT LAST("+metric+") FROM \""+dbname+"\" WHERE hostname = '"+hostname+"' and time < now() - "+(param+1)+ unit;
		}
		Object result = null;
		QueryResult queryResult = service.query(str);
		//将字符串传至influxdb，判断SQL语句
		if(queryResult.equals(null)){
			return 0.0;
		}else{
			try {
				result =  queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(1);
				
			} catch (Exception e) {
				influDBClient.builder().close();
				return 0.0;
			}
		
		//将查询结果赋值给result
		influDBClient.builder().close();
		//关闭与influxdb的连接
		return result;//返回函数计算结果
		}
	}


	/* (non-Javadoc)
	 * @see com.nwpu.bigdata.util.FuncUtil#sum(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public Object sum(int param, String unit, String hostname, String dbname, String metric) {
		// TODO Auto-generated method stub

		//求平均的方法
		InfluxdbService service = new InfluxdbService(k8s,k8sretentionPolicy,influDBClient.builder());
		//连接influxdb数据库
		if(service.equals(null))			
		{
			System.out.println(service.equals(null));
			return null;
		}
		//判断输入是否为空
		String str = null;
		//先将str设为空

		if(unit == "num"){
			str =  "SELECT SUM("+metric+") FROM \""+dbname+"\" WHERE time < now() - "+(param+1)+"m";
		}else{
			str =  "SELECT SUM("+metric+") FROM \""+dbname+"\" WHERE time < now() - "+(param+1)+ unit;
		}
		Object result = null;
		QueryResult queryResult = service.query(str);
		//将字符串传至influxdb，判断SQL语句
		if(queryResult.equals(null)){
			return 0.0;
		}else{
			try {
				result =  queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(1);
				
			} catch (Exception e) {
				influDBClient.builder().close();
				return 0.0;
			}
		
		//将查询结果赋值给result
		influDBClient.builder().close();
		//关闭与influxdb的连接
		return result;//返回函数计算结果
		}
	
	}

}
